<template>
  <div class="container">
    <div class="search-box">
      <el-input
        placeholder="请输入内容"
        class="input-box"
        v-model="addfrpo"
      ></el-input>
      <el-button @click="eliMinate">清除</el-button>
      <el-button type="primary" @click="seaRch">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="list">
      <el-table-column type="index" label="任务编号" width="80">
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template v-slot="scope">
          <el-button
            size="mini"
            style="margin-right: 10px"
            @click="editList(scope.row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            @confirm="delList(scope.row.id)"
            title="确定删除任务吗？"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query._page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="query._limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <el-button size="small" plain @click="newlyAdded">+ 新增</el-button>
    </div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      @close="closeoooo"
    >
      <el-form ref="addForm" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="任务描述" prop="des">
          <el-input v-model="form.des"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="adpsseeFaaa">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { v4 as uuidv4 } from "uuid";
import {
  fetchDepartmentList,
  delDepartmentList,
  getmentList,
  getDepartmentList,
  postDepartmentList,
  updatePermission,
} from "@/api/Interface.js";
export default {
  data() {
    return {
      title: "",
      total: 0,
      form: {
        name: "",
        des: "",
        id: "",
      },
      query: {
        _page: 1,
        _limit: 5,
      },
      addfrpo: "",
      list: [],
      dialogVisible: false,
      rules: {
        name: [
          { required: true, message: "任务名称为必填项", trigger: "blur" },
        ],
        des: [{ required: true, message: "任务描述为必填项", trigger: "blur" }],
      },
    };
  },
  mounted() {
    this.addEventListener();
  },
  methods: {
    // 获取列表按钮
    async addEventListener() {
      const res = await fetchDepartmentList(this.query);
      // console.log(res)
      this.list = res.data;
      this.total = Number(res.headers["x-total-count"]);
      // console.log(res);
    },
    // 改变了每页数据条数的时候，触发这个事件
    handleSizeChange(newSize) {
      this.query._limit = newSize;
      this.query._page = 1;
      this.addEventListener();
    },
    // 页码改变的时候，触发的事件
    handleCurrentChange(newPage) {
      // console.log(newPage) // 表示点击的，或者跳转的最新的页码值
      this.query._page = newPage;
      this.addEventListener();
    },
    // 删除按钮
    async delList(id) {
      // console.log(id);
      await delDepartmentList(id);
      this.$message.success("删除成功");
      this.addEventListener();
    },
    //搜索按钮
    async seaRch() {
      // console.log(this.addfrpo);
      const res = await getmentList(this.addfrpo);
      // console.log(res);
      this.list = res.data;
      // console.log(this.list);
      // this.addEventListener();
    },
    //清除按钮
    eliMinate() {
      this.addfrpo = "";
      this.addEventListener();
    },
    // 编辑按钮
    async editList(id) {
      this.title = "编辑";
      this.dialogVisible = true;
      const res = await getDepartmentList(id);
      this.form = res.data;
    },
    closeoooo() {
      this.dialogVisible = false;
      this.form = {
        name: "",
        des: "",
        id: "",
      };
    },
    newlyAdded() {
      this.title = "新增";
      this.dialogVisible = true;

      // console.log(this.form.id);
    },
    //确定按钮
    adpsseeFaaa() {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          if (this.form.id) {
            console.log(this.form.id);
            await updatePermission(this.form);
            this.$message.success("编辑部门成功");
          } else {
            this.form.id = uuidv4();
            await postDepartmentList(this.form);
            this.$message.success("新增部门成功");
          }
          this.dialogVisible = false;
          this.addEventListener();
          // 这个方法有点坑
          this.$refs.addForm.resetFields();
          // 重置表单 防止id未被清空 导致判断失效
          this.form = {
            name: "",
            des: "",
            id: "",
          };
        }
      });
    },
  },
};
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
